package org.bronycorps.mlptss.utils;

import java.util.HashMap;
import java.util.Map;

import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.Array;

public class DrawableAnimation extends Animation {

	private final Rectangle bounds;

	protected final Map<TextureRegion, TextureRegionDrawable> drawables = new HashMap<TextureRegion, TextureRegionDrawable>();

	private final Vector2 origin;

	public DrawableAnimation(float frameDuration,
			Array<? extends TextureRegion> keyFrames, int playType,
			Vector2 origin, Rectangle bounds) {
		super(frameDuration, keyFrames, playType);
		this.origin = origin;
		this.bounds = bounds;
		createDrawables(keyFrames);
	}

	public DrawableAnimation(float frameDuration,
			Array<? extends TextureRegion> keyFrames, Vector2 origin,
			Rectangle bounds) {
		super(frameDuration, keyFrames);
		this.origin = origin;
		this.bounds = bounds;
		createDrawables(keyFrames);
	}

	public DrawableAnimation(float frameDuration, Vector2 origin,
			Rectangle bounds, TextureRegion... keyFrames) {
		super(frameDuration, keyFrames);
		this.origin = origin;
		this.bounds = bounds;
		createDrawables(new Array<TextureRegion>(keyFrames));
	}

	private void createDrawables(Array<? extends TextureRegion> keyFrames) {
		for (TextureRegion textureRegion : keyFrames) {
			this.drawables.put(textureRegion, new TextureRegionDrawable(
					textureRegion));
		}
	}

	public Rectangle getBounds() {
		return this.bounds;
	}

	public TextureRegionDrawable getDrawableKeyFrame(float stateTime) {
		return this.drawables.get(this.getKeyFrame(stateTime));
	}

	public Map<TextureRegion, TextureRegionDrawable> getDrawables() {
		return this.drawables;
	}

	public Vector2 getOrigin() {
		return this.origin;
	}

}
